from rest_framework import serializers
from .models import User, Goods, Inventory, Customer, SalesOrder, SaleDetail, Report

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['id', 'username', 'password', 'role']  # 显示的字段
        extra_kwargs = {
            'password': {'write_only': True},  # 密码字段只写入
            'role': {'default': 'user'},  # 默认角色是 "user"
        }

    def create(self, validated_data):
        user = User.objects.create_user(
            username=validated_data['username'],
            password=validated_data['password'],
            role=validated_data.get('role', 'user')
        )
        return user

    def update(self, instance, validated_data):
        instance.username = validated_data.get('username', instance.username)
        instance.role = validated_data.get('role', instance.role)
        instance.save()
        return instance

class GoodsSerializer(serializers.ModelSerializer):
    quantity = serializers.SerializerMethodField()
    inventory_quantity = serializers.SerializerMethodField()
    class Meta:
        model = Goods
        fields = '__all__'

    def get_quantity(self, obj):
        # 获取商品已经上架的数量
        try:
            inventory = Inventory.objects.get(goods=obj)
            return inventory.total_inventory - inventory.current_quantity
        except Inventory.DoesNotExist:
            return 0

    def get_inventory_quantity(self, obj):
        # 获取商品库存数量
        try:
            inventory = Inventory.objects.get(goods=obj)
            return inventory.current_quantity
        except Inventory.DoesNotExist:
            return 0

class InventorySerializer(serializers.ModelSerializer):
    goods_name = serializers.SerializerMethodField()
    class Meta:
        model = Inventory
        fields = '__all__'

    def get_goods_name(self, obj):
        return obj.goods.goods_name

class CustomerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Customer
        fields = '__all__'

class SaleDetailSerializer(serializers.ModelSerializer):
    goods_name = serializers.SerializerMethodField()
    class Meta:
        model = SaleDetail
        fields = '__all__'

    def get_goods_name(self, obj):
        # 获取商品名称
        if obj.goods:
            return obj.goods.goods_name
        return None

class SalesOrderSerializer(serializers.ModelSerializer):
    customer_name = serializers.CharField(source='customer.customer_name', read_only=True)
    sales_detail = serializers.SerializerMethodField()
    status_cn = serializers.SerializerMethodField()
    class Meta:
        model = SalesOrder
        fields = '__all__'

    def get_sales_detail(self, obj):
        # 获取销售订单的详情
        sales_detail = SaleDetail.objects.filter(order=obj)
        return SaleDetailSerializer(sales_detail, many=True).data

    def get_status_cn(self, obj):
        # 根据 status 字段返回中文描述
        status_map = {
            'pending': '待处理',
            'shipped': '已发货',
            'completed': '已完成',
        }
        return status_map.get(obj.status, '待处理')

class ReportSerializer(serializers.ModelSerializer):
    class Meta:
        model = Report
        fields = '__all__'